package com.resource.spring;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.resource.utils.ServiceException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.MapUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 断言
 * @Author: xianggaoxiong
 * @Date: 16:23 2020/9/25
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiAssert {

    /**
     * obj1 eq obj2
     *
     * @param obj1
     * @param obj2
     * @param errorMsg
     */
    public static void equals(String errorMsg, Object obj1, Object obj2) {
        if (Objects.equals(obj1, obj2)) {
            failure(errorMsg);
        }
    }

    public static void isTrue(String errorMsg, boolean condition) {
        if (condition) {
            failure(errorMsg);
        }
    }

    public static void isFalse(String errorMsg, boolean condition) {
        if (!condition) {
            failure(errorMsg);
        }
    }

    public static void isNull(String errorMsg, Object... conditions) {
        if (ObjectUtil.isNull(conditions)) {
            failure(errorMsg);
        }
    }

    public static void notNull(String errorMsg, Object... conditions) {
        if (ObjectUtil.isNotNull(conditions)) {
            failure(errorMsg);
        }
    }

    /**
     * <p>
     * 失败结果
     * </p>
     *
     * @param errorMsg 异常错误码
     */
    public static void failure(String errorMsg) {
        throw new ServiceException(errorMsg);
    }

    public static void notEmpty(String errorMsg, Object[] array) {
        if (ObjectUtil.isNotEmpty(array)) {
            failure(errorMsg);
        }
    }

    /**
     * Assert that an array has no null elements. Note: Does not complain if the
     * array is empty!
     * <p>
     * <pre class="code">
     * Assert.noNullElements(array, &quot;The array must have non-null elements&quot;);
     * </pre>
     *
     * @param array     the array to check
     * @param errorMsg the exception message to use if the assertion fails
     * @throws ServiceException if the object array contains a {@code null} element
     */
    public static void noNullElements(String errorMsg, Object[] array) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    failure(errorMsg);
                }
            }
        }
    }

    /**
     * Assert that a collection has elements; that is, it must not be
     * {@code null} and must have at least one element.
     * <p>
     * <pre class="code">
     * Assert.notEmpty(collection, &quot;Collection must have elements&quot;);
     * </pre>
     *
     * @param collection the collection to check
     * @param errorMsg  the exception message to use if the assertion fails
     * @throws ServiceException if the collection is {@code null} or has no elements
     */
    public static void notEmpty(String errorMsg, Collection<?> collection) {
        if (CollUtil.isNotEmpty(collection)) {
            failure(errorMsg);
        }
    }

    /**
     * Assert that a Map has entries; that is, it must not be {@code null} and
     * must have at least one entry.
     * <p>
     * <pre class="code">
     * Assert.notEmpty(map, &quot;Map must have entries&quot;);
     * </pre>
     *
     * @param map       the map to check
     * @param errorMsg the exception message to use if the assertion fails
     * @throws ServiceException if the map is {@code null} or has no entries
     */
    public static void notEmpty(String errorMsg, Map<?, ?> map) {
        if (MapUtils.isNotEmpty(map)) {
            failure(errorMsg);
        }
    }

    /**
     * 判断字符串是空
     * @param errorMsg
     * @param str
     */
    public static void isEmpty(String errorMsg, CharSequence str) {
        if (StrUtil.isEmpty(str)) {
            failure(errorMsg);
        }
    }
    /**
     * Assert that a collection has elements; that is, it must not be
     * {@code null} and must have at least one element.
     * <p>
     * <pre class="code">
     * Assert.notEmpty(collection, &quot;Collection must have elements&quot;);
     * </pre>
     *
     * @param collection the collection to check
     * @param errorMsg  the exception message to use if the assertion fails
     * @throws ServiceException if the collection is {@code null} or has no elements
     */
    public static void isEmpty(String errorMsg, Collection<?> collection) {
        if (CollUtil.isEmpty(collection)) {
            failure(errorMsg);
        }
    }

    /**
     * Assert that a Map has entries; that is, it must not be {@code null} and
     * must have at least one entry.
     * <p>
     * <pre class="code">
     * Assert.notEmpty(map, &quot;Map must have entries&quot;);
     * </pre>
     *
     * @param map       the map to check
     * @param errorMsg the exception message to use if the assertion fails
     * @throws ServiceException if the map is {@code null} or has no entries
     */
    public static void isEmpty(String errorMsg, Map<?, ?> map) {
        if (MapUtils.isEmpty(map)) {
            failure(errorMsg);
        }
    }

}
